const express = require('express')
let app = express()
const session = require('express-session')
const mongostore = require('connect-mongo')
const bodyparser = require('body-parser')
const cors = require('cors')
const router = require('./router/index')

app.use(express.static(__dirname))
// 解析请求体中的json与urlencoded
app.use(express.json())
app.use(express.urlencoded({ extends: false }))

// 为请求报文挂在上session
app.use(session({
  secret: 'liangweijun',
  resave: true,
  saveUninitialized: false,
  store: mongostore.create({
    mongoUrl: 'mongodb://127.0.0.1:27017/mymongodb'
  }),
  cookie: {
    secure: true,
    maxAge: 1000 * 60 * 5
  }
}))
// 为请求报文挂在上body
app.use(bodyparser.urlencoded({ extended: false }))
app.use(bodyparser.json())
// 以下路由能实现跨域
app.use(cors())

app.use(router)

app.listen(9000, function () {
  console.log('9000端口的监听已开启');
})
